package com.sqmf.action;

import com.alibaba.fastjson.JSONObject;
import com.sqmf.pojo.User;
import com.sqmf.service.UserService;
import com.sqmf.util.Result;
import com.sqmf.util.ResultCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Validated
@RestController
@RequestMapping("/api/user")
public class UserController {
    private final static Logger logger = LoggerFactory.getLogger(IndexController.class);
    private JSONObject jsonObject = null;//接收json
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/getUserAll", method = RequestMethod.GET)
    public Result getUserAll() {
        try {
            logger.info("*********************查询用户列表**************************");
            List<User> list = userService.getAll();
            logger.info("用户数：" + list.size());
            logger.info(list.toString());
            return Result.SUCCESS(list);
        } catch (Exception $exception) {
            logger.error("user_error:" + $exception.getMessage());
            return Result.API_SERVER_ERROR();
        }
    }

    @RequestMapping(value = "/findUserInfoById", method = RequestMethod.GET)
    public Result findUserInfoById(Integer id) {
        try {
            logger.info("*********************查询用户信息（id=" + id + "）**************************");
            User user = userService.findUserInfoById(id);
            logger.info(user.toString());
            return Result.SUCCESS(user);
        } catch (Exception $exception) {
            logger.error("user_error:" + $exception.getMessage());
            return Result.ERROR(40001, $exception.getMessage());
        }
    }


    @PostMapping(value = "/update", produces = "application/json;charset=UTF-8")
    public Result update(@RequestBody User user) {
        try {
            Integer ret = userService.updateUser(user);
            logger.info(ret + "");
            return Result.SUCCESS(ret);
        } catch (Exception $exception) {
            logger.error("user_error:" + $exception.getMessage());
            return Result.ERROR(40001, $exception.getMessage());
        }
    }
}
